// SpriteView.cpp : implementation file
//

#include "stdafx.h"
#include "SpriteTool.h"
#include "SpriteDoc.h"
#include "SpriteView.h"
#include "MemDC.h"
#include "ChildFrm.h"
#include "LeftView.h"
#include "FclipDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define VIEW_ORG_X	16
#define VIEW_ORG_Y	16

/////////////////////////////////////////////////////////////////////////////
// CSpriteView

IMPLEMENT_DYNCREATE(CSpriteView, CScrollView)

BEGIN_MESSAGE_MAP(CSpriteView, CScrollView)
	//{{AFX_MSG_MAP(CSpriteView)
	ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDBLCLK()
	ON_COMMAND(ID_MAKE_DN, OnMakeDn)
	ON_COMMAND(ID_MAKE_UP, OnMakeUp)
	ON_WM_RBUTTONDOWN()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


CSpriteView::CSpriteView()
{
	m_nZoom = 0;
}

CSpriteView::~CSpriteView()
{
}

BOOL CSpriteView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CScrollView::PreCreateWindow(cs);
}


/////////////////////////////////////////////////////////////////////////////
// CSpriteView drawing

void CSpriteView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	sizeTotal.cx = sizeTotal.cy = 300;
	SetScrollSizes(MM_TEXT, sizeTotal);

	VERIFY(m_brBkgnd.CreateSolidBrush(RGB(128, 128, 128)));
	VERIFY(m_brHatch.CreateHatchBrush(HS_CROSS, RGB(191, 191, 191)));
}

void CSpriteView::OnDraw(CDC* pDC)
{
	POINT ptOrg;
	ptOrg.x = VIEW_ORG_X;
	ptOrg.y = VIEW_ORG_Y;

	CSize tSize = GetTotalSize();

	RECT rect;
	GetClientRect(&rect);
	rect.left = 0;
	rect.top = 0;
	if(rect.right < tSize.cx)
		rect.right = tSize.cx;
	if(rect.bottom < tSize.cy)
		rect.bottom = tSize.cy;


	CMemDC* pMemDC = new CMemDC(pDC, rect);
	pMemDC->EraseBkgnd(m_brBkgnd, m_brHatch, VIEW_ORG_X, VIEW_ORG_Y);

	CSpriteDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if(pDoc->m_pSelFrame != NULL)
	{
		FRAME* pFrame = pDoc->m_pSelFrame;
		pFrame->OnDraw(pMemDC, VIEW_ORG_X, VIEW_ORG_X, m_nZoom);
	}
	
	delete pMemDC;

}

/////////////////////////////////////////////////////////////////////////////
// CSpriteView diagnostics

#ifdef _DEBUG
void CSpriteView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CSpriteView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CSpriteDoc* CSpriteView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSpriteDoc)));
	return (CSpriteDoc*)m_pDocument;
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSpriteView message handlers

BOOL CSpriteView::OnEraseBkgnd(CDC* pDC) 
{
	return 1;
}

void CSpriteView::OnMouseMove(UINT nFlags, CPoint point) 
{
	CSpriteDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if(nFlags & MK_LBUTTON)
	{
		FCLIP* pFclip = pDoc->m_pSelFclip;
		if(pFclip != NULL && pFclip->bLock == FALSE)
		{
			CPoint pt = point;
			pt.x -= VIEW_ORG_X;
			pt.y -= VIEW_ORG_Y;

			if(m_nZoom > 1)
			{
				pt.x /= m_nZoom;
				pt.y /= m_nZoom;
			}

			
			if(m_bDragging == FALSE)
			{
				RECT rect;
				pFclip->GetRect(rect);

				if(PtInRect(&rect, pt))
				{
					m_ptOffset.x = pt.x - rect.left;
					m_ptOffset.y = pt.y - rect.top;

					m_bDragging = TRUE;
				}
			}

			if(m_bDragging)
			{
				pt.x = pt.x - m_ptOffset.x;
				pt.y = pt.y - m_ptOffset.y;
				
				pFclip->nPosX = pt.x > 0 ? pt.x : 0;
				pFclip->nPosY = pt.y > 0 ? pt.y : 0;
				Invalidate();
			}
		}
	}
	else
	{
		m_bDragging = FALSE;
	}
	
	CScrollView::OnMouseMove(nFlags, point);
}

void CSpriteView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CChildFrame* pFrameWnd = (CChildFrame*)GetParentFrame();
	if(pFrameWnd)
	{
		CLeftView* pLeftView = pFrameWnd->GetLeftView();
		int x = point.x - VIEW_ORG_X;
		int y = point.y - VIEW_ORG_Y;
		if(m_nZoom > 1)
		{
			x /= m_nZoom;
			y /= m_nZoom;
		}
		pLeftView->HitSelect(x, y);
	}
	
	CScrollView::OnLButtonDown(nFlags, point);
}

void CSpriteView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_bDragging = FALSE;
	
	CScrollView::OnLButtonUp(nFlags, point);
}

void CSpriteView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	OnLButtonDown(nFlags, point);

	CSpriteDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	FCLIP* pFclip = pDoc->m_pSelFclip;
	if(pFclip != NULL)
	{
		CFclipDlg dlg(this);
		dlg.m_pFclip = pFclip;
		dlg.DoModal();
		Invalidate();
	}
	
	CScrollView::OnLButtonDblClk(nFlags, point);
}

void CSpriteView::OnMakeDn() 
{
	CChildFrame* pFrameWnd = (CChildFrame*)GetParentFrame();
	if(pFrameWnd)
	{
		CLeftView* pLeftView = pFrameWnd->GetLeftView();
		pLeftView->OnSetFclipDn();
	}
}

void CSpriteView::OnMakeUp() 
{
	CChildFrame* pFrameWnd = (CChildFrame*)GetParentFrame();
	if(pFrameWnd)
	{
		CLeftView* pLeftView = pFrameWnd->GetLeftView();
		pLeftView->OnSetFclipUp();
	}

}

void CSpriteView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CScrollView::OnRButtonDown(nFlags, point);
}

void CSpriteView::OnZoomOut() 
{
	if(m_nZoom <= 1)
	{
		m_nZoom = 2;
	}
	else
	{
		m_nZoom++;
	}
	Invalidate();
}

void CSpriteView::OnZoomIn() 
{
	if(m_nZoom > 1)
	{
		m_nZoom--;
	}
	else
	{
		m_nZoom = 0;
	}
	Invalidate();
}

void CSpriteView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	CSpriteDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	if(GetKeyState(VK_CONTROL) & 0x80)
	{
		FRAME* pFrame = pDoc->m_pSelFrame;
		if(pFrame != NULL)
		{
			RECT rect;
			pFrame->GetRect(rect);

			int x = 0;
			int y = 0;

			switch(nChar)
			{
			case VK_UP:
				if(rect.top > 0)
					y--;
				break;
			case VK_DOWN:
				y++;
				break;
			case VK_LEFT:
				if(rect.left > 0)
					x--;
				break;
			case VK_RIGHT:
				x++;
				break;
			}
			if(x != 0 || y != 0)
			{
				pFrame->Offset(x, y);
				Invalidate();
			}

		}
	}
	else
	{
		FCLIP* pFclip = pDoc->m_pSelFclip;
		if(pFclip != NULL && pFclip->bLock == FALSE)
		{
			int x = pFclip->nPosX;
			int y = pFclip->nPosY;

			switch(nChar)
			{
			case VK_UP:
				y--;
				pFclip->nPosY = y > 0 ? y : 0;
				Invalidate();
				break;
			case VK_DOWN:
				y++;
				pFclip->nPosY = y > 0 ? y : 0;
				Invalidate();
				break;
			case VK_LEFT:
				x--;
				pFclip->nPosX = x > 0 ? x : 0;
				Invalidate();
				break;
			case VK_RIGHT:
				x++;
				pFclip->nPosX = x > 0 ? x : 0;
				Invalidate();
				break;
			}
		}
	}
	CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CSpriteView::OnFlipY(BOOL bFrame)
{
	CSpriteDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	FRAME* pFrame = pDoc->m_pSelFrame;
	if(pFrame && bFrame)
	{
		pFrame->FlipY();
		Invalidate();
	}
	else if(pDoc->m_pSelAction)
	{
		ACTION* pAction = pDoc->m_pSelAction;
		list<FRAME*>::iterator it;
		it = pAction->FrameList.begin();
		while(it != pAction->FrameList.end())
		{
			FRAME* pFrame = *it++;
			pFrame->FlipY();
		}
		Invalidate();
	}
}